<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <date-phase ref="datePhase" dateTypeRange="234" :datePhase="datePhase" ></date-phase>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchItem()">查询</el-button>
        <el-button type="primary" @click="exportExcel()">导出</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-table :height="tableHeight" ref="table" :data="tableData"  border style="width: 100%" >
        <el-table-column label="时间" prop="date" min-width="180" align="center"></el-table-column>
        <el-table-column label="整体业绩" prop="totalSale" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.totalSale? scope.row.totalSale: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="环比增长" prop="totalSaleGrowth" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.totalSaleGrowth? scope.row.totalSaleGrowth + '%': '0%'}}
          </template>
        </el-table-column>
        <el-table-column label="平台业绩" prop="platSale" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.platSale? scope.row.platSale: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="环比增长" prop="platSaleGrowth" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.platSaleGrowth? scope.row.platSaleGrowth + '%': '0%'}}
          </template>
        </el-table-column>
        <el-table-column label="市场业绩" prop="marketSale" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.marketSale? scope.row.marketSale: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="环比增长" prop="marketSaleGrowth" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.marketSaleGrowth? scope.row.marketSaleGrowth + '%': '0%'}}
          </template>
        </el-table-column>
        <el-table-column label="市场折扣" prop="marketDiscount" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.marketDiscount? scope.row.marketDiscount: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="市场广告" prop="marketAd" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.marketAd? scope.row.marketAd: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="市场广告占比" prop="marketAdPercent" min-width="110" align="center">
          <template slot-scope="scope">
            {{scope.row.marketAdPercent? scope.row.marketAdPercent + '%': '0%'}}
          </template>
        </el-table-column>
        <el-table-column label="平台广告" prop="platAd" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.platAd? scope.row.platAd: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="平台广告占比" prop="platAdPercent" min-width="110" align="center">
          <template slot-scope="scope">
            {{scope.row.platAdPercent? scope.row.platAdPercent + '%': '0%'}}
          </template>
        </el-table-column>
        <el-table-column label="PI数" prop="PINum" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.PINum? scope.row.PINum: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="新增客户数" prop="newCustomerNum" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.newCustomerNum? scope.row.newCustomerNum: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="新客户业绩" prop="newCustomerSale" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.newCustomerSale? scope.row.newCustomerSale: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="新客户折扣" prop="newCustomerDiscount" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.newCustomerDiscount? scope.row.newCustomerDiscount: '0'}}
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
  import DatePhase from  '@/components/sale/date-phase'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        datePhase:{
            dateType: 2,
            value:'',
            to:''
        },
        tableHeight: window.tableHeight,
        tableData:[]
      }
    },
    components: {
      DatePhase
    },
    activated() {
      this.tableHeight = document.documentElement['clientHeight'] - 220
      window.onresize = () => {
        this.tableHeight = document.documentElement['clientHeight'] - 220
      }
    },
    mounted(){
      this.searchItem()
    },
    methods: {
      async searchItem() {
        this.tableData = await this.$httpN.postB('mpb/report/summary',{
          from: this.datePhase.value,
          to: this.datePhase.to,
          dateType: this.datePhase.dateType
        })
      },
      exportExcel(){
        this.$nextTick(function () {
          let wb = XLSX.utils.table_to_book(this.$refs.table.$el);
          let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          try {
            FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '业绩汇总报表.xlsx')
          } catch (e) {
            if (typeof console !== 'undefined') console.log(e, wbout)
          }
          return wbout;
        });
      }
    }
  }
</script>
